import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    isLogin: localStorage.getItem('isLogin') === 'true',
    userRole: localStorage.getItem('userRole') || ''
  },
  mutations: {
    setLoginStatus(state, status) {
      state.isLogin = status;
      localStorage.setItem('isLogin', status);
    },
    setUserRole(state, role) {
      state.userRole = role;
      localStorage.setItem('userRole', role);
    },
    logout(state) {
      state.isLogin = false;
      state.userRole = '';
      localStorage.removeItem('isLogin');
      localStorage.removeItem('userRole');
    }
  },
  actions: {
    login({ commit }, { role }) {
      commit('setLoginStatus', true);
      commit('setUserRole', role);
    },
    logout({ commit }) {
      commit('logout');
    }
  },
  getters: {
    isLogin: state => state.isLogin,
    userRole: state => state.userRole
  }
});